package com.apotik.legenda.panel;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.BorderLayout;

import javax.swing.SwingConstants;

import java.awt.GridLayout;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableCellRenderer;

import com.apotik.legenda.bean.TrxUniqueNumberBean;
import com.apotik.legenda.custom.TableModelCustom;
import com.apotik.legenda.dao.ProductDao;
import com.apotik.legenda.dao.SalesTrxDao;
import com.apotik.legenda.dao.SalesTrxObatDao;
import com.apotik.legenda.dao.TransactionUniqueNumberDao;
import com.apotik.legenda.dialog.AddDokterToTrxDialog;
import com.apotik.legenda.dialog.AddProductToTrxDialog;
import com.apotik.legenda.dialog.EditTrxObatDiscountDialog;
import com.apotik.legenda.dialog.EditTrxObatQuantityDialog;
import com.apotik.legenda.model.Product;
import com.apotik.legenda.model.SalesTrx;
import com.apotik.legenda.model.SalesTrxObat;
import com.apotik.legenda.model.Shift;
import com.apotik.legenda.model.TransactionUniqueNumber;
import com.apotik.legenda.model.User;
import com.apotik.legenda.observer.PosObserver;
import com.apotik.legenda.util.AppUtil;
import com.apotik.legenda.util.CalendarUtil;

import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Vector;
import java.awt.event.KeyEvent;

import javax.swing.JTextField;
import javax.swing.border.LineBorder;


public class PosPanel1 extends JPanel implements ActionListener, PosObserver, FocusListener, KeyListener {
	private static final long serialVersionUID = 1L;
	
	public static final int UPDATE_QUANTITY = 101;
	public static final int UPDATE_DISCOUNT = 102;
	
	private JSplitPane splitPane;
	
	private JLabel lblNoNotaValue;
	private JLabel lblTotal;
	
	private JButton btnTrxNew;
	private JButton btnBayar;
	private JButton btnBatal;
	private JButton btnAddProd;
	private JButton btnEditQuantity;
	private JButton btnEditDiscount;
	private JButton btnHapusProd;
	private JButton btnAddEditDokter;
	private JButton btnDelDokter;
	private JButton btnAddEditCust;
	private JButton btnDelCust;
	
	private JTextField textFieldBiayaDokter;
	private JTextField textFieldBiayaTindakan;
	private JTextField textFieldBiayaLab;
	private JTextField textFieldBiayaAdm;
	private JTextField textFieldRU;
	private JTextField textFieldMisc;
	private JTextField textFieldTotalObat;
	
	private JTable table;
	private TableModelCustom tableModel;
	
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxTrxType;
	
	private User user;
	private Shift shift;
	private SalesTrx currentSalesTrx;
	private int modelRow  = -1;
	private JTextField textFieldDiskonPersen;
	private JTextField textFieldDiskonRp;
	private JTextField textFieldSubTotal;
	private JTextField textFieldDokter;
	private JTextField textFieldCust;
	
	public PosPanel1(User user, Shift shift) {
		this.user = user;
		this.shift = shift;
		
		JPanel leftComp = leftComponents();
		JPanel rightComp = rightComponents();
		
		firstConditionForComp();
		
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComp, rightComp);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(180);
	}
	
	private JPanel leftComponents(){
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		String timeStart = CalendarUtil.dateToString_HHMM(shift.getStartTime());
		String timeFinish = CalendarUtil.dateToString_HHMM(shift.getFinishTime());
		
		String kasirValue = user.getFullName();
		String shiftValue = shift.getShiftName()+" ("+timeStart+" - "+timeFinish+")";
		
		btnTrxNew = new JButton("Transaksi Baru");
		panel.add(btnTrxNew);
		btnTrxNew.setBounds(10, 10, 160, 30);
		btnTrxNew.addActionListener(this);
		
		btnBayar = new JButton("Bayar");
		panel.add(btnBayar);
		btnBayar.setBounds(10, 49, 160, 30);
		btnBayar.addActionListener(this);
		
		btnBatal = new JButton("Batal");
		panel.add(btnBatal);
		btnBatal.setEnabled(false);
		btnBatal.setBounds(10, 86, 160, 30);
		btnBatal.addActionListener(this);
		
		JSeparator separator1 = new JSeparator();
		separator1.setBackground(new Color(192, 192, 192));
		separator1.setBounds(10, 126, 160, 2);
		panel.add(separator1);
		
		JLabel lblKasir = new JLabel("Kasir: ");
		lblKasir.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblKasir.setBounds(10, 136, 159, 20);
		panel.add(lblKasir);
		
		JLabel lblKasirValue = new JLabel(kasirValue);
		lblKasirValue.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblKasirValue.setBounds(10, 154, 160, 23);
		panel.add(lblKasirValue);
		
		JLabel lblShift = new JLabel("Shift: ");
		lblShift.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblShift.setBounds(10, 182, 46, 20);
		panel.add(lblShift);
		
		JLabel lblShiftvalue = new JLabel(shiftValue);
		lblShiftvalue.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblShiftvalue.setBounds(10, 200, 160, 23);
		panel.add(lblShiftvalue);
		
		JSeparator separator2 = new JSeparator();
		separator2.setBackground(Color.LIGHT_GRAY);
		separator2.setBounds(10, 233, 160, 2);
		panel.add(separator2);
		
		return panel;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked"})
	private JPanel rightComponents(){
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		panel_1.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new EmptyBorder(10, 0, 10, 0));
		panel_1.add(panel_3, BorderLayout.WEST);
		panel_3.setLayout(new GridLayout(2, 3));
		
		JLabel lblNoNota = new JLabel("  Nomor NOTA : ");
		lblNoNota.setFont(new Font("Tahoma", Font.BOLD, 14));
		panel_3.add(lblNoNota);
		
		lblNoNotaValue = new JLabel("-");
		lblNoNotaValue.setFont(new Font("Tahoma", Font.BOLD, 14));
		panel_3.add(lblNoNotaValue);
		
		JLabel lblTitikDua2 = new JLabel(": ");
		lblTitikDua2.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		JLabel lblTypeTrx = new JLabel("  Type Transaksi : ");
		lblTypeTrx.setFont(new Font("Tahoma", Font.BOLD, 14));
		panel_3.add(lblTypeTrx);
		
		comboBoxTrxType = new JComboBox();
		comboBoxTrxType.setModel(new DefaultComboBoxModel(new String[] {"-", AppUtil.TYPE_TRX_BEBAS, AppUtil.TYPE_TRX_RESEP}));
		panel_3.add(comboBoxTrxType);
		
		JPanel panel_4 = new JPanel();
		panel_4.setPreferredSize(new Dimension(300, 0));
		panel_4.setBackground(new Color(0, 0, 255));
		panel_1.add(panel_4, BorderLayout.EAST);
		
		lblTotal = new JLabel("0");
		lblTotal.setForeground(new Color(255, 255, 0));
		lblTotal.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTotal.setFont(new Font("Tahoma", Font.BOLD, 33));
		lblTotal.setToolTipText("Total");
		lblTotal.setBorder(new EmptyBorder(10, 0, 0, 2));
		panel_4.add(lblTotal);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout());
		
		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5, BorderLayout.NORTH);
		panel_5.setLayout(new BorderLayout());
		
		JPanel panel_7 = new JPanel();
		panel_5.add(panel_7, BorderLayout.WEST);
		
		btnAddProd = new JButton("Tambah");
		btnAddProd.addActionListener(this);
		btnAddProd.setMnemonic(AppUtil.MNEMONIC_TAMBAH);
		btnAddProd.setToolTipText("ALT + T");
		panel_7.add(btnAddProd);
		
		btnEditQuantity = new JButton("Edit Quantity");
		btnEditQuantity.addActionListener(this);
		btnEditQuantity.setMnemonic(KeyEvent.VK_Q);
		btnEditQuantity.setToolTipText("ALT + Q");
		panel_7.add(btnEditQuantity);
		
		btnEditDiscount = new JButton("Edit Discount");
		btnEditDiscount.addActionListener(this);
		btnEditDiscount.setMnemonic(KeyEvent.VK_D);
		btnEditDiscount.setToolTipText("ALT + D");
		panel_7.add(btnEditDiscount);
		
		btnHapusProd = new JButton("Hapus");
		btnHapusProd.addActionListener(this);
		btnHapusProd.setMnemonic(AppUtil.MNEMONIC_HAPUS);
		btnHapusProd.setToolTipText("ALT + H");
		panel_7.add(btnHapusProd);
		
		JScrollPane panel_6 = panel_6();
		panel_2.add(panel_6, BorderLayout.CENTER);
		
		JPanel panel_8 = new JPanel();
		panel.add(panel_8, BorderLayout.SOUTH);
		panel_8.setBorder(new LineBorder(new Color(192, 192, 192), 1, true));
		BorderLayout bl_panel_8 = new BorderLayout();
		bl_panel_8.setVgap(10);
		bl_panel_8.setHgap(10);
		panel_8.setLayout(bl_panel_8);
		
		JPanel panel_9 = panel_9();
		panel_8.add(panel_9, BorderLayout.EAST);
		
		return panel;
	}
	
	private void firstConditionForComp(){
		btnTrxNew.setEnabled(true);
		btnBayar.setEnabled(false);
		btnBatal.setEnabled(false);
		comboBoxTrxType.setEnabled(false);
		lblNoNotaValue.setText("-");
		btnAddProd.setEnabled(false);
		btnEditQuantity.setEnabled(false);
		btnEditDiscount.setEnabled(false);
		btnHapusProd.setEnabled(false);
		
		tableModel.removeRowAll();
		currentSalesTrx = null;
		modelRow = -1;
	}
	
	@SuppressWarnings("serial")
	private JScrollPane panel_6(){
		JScrollPane scrollPane = new JScrollPane();
		
		String[] columnNames = {"Kode Barang", "Nama Barang", "Quantity", "Harga Satuan (Rp)", "SC (Rp)", "Discount (%)", "Jumlah (Rp)"};
		tableModel = new TableModelCustom(columnNames);
		
		table = new JTable(){
			@Override
			public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
				JLabel label = (JLabel) super.prepareRenderer(renderer, row, column);
				
				if(column==2 || column==3 || column==4 || column==5 || column==6){
					label.setHorizontalAlignment(JLabel.RIGHT);
				}else{
					label.setHorizontalAlignment(JLabel.LEFT);
				}
				
				return label;
			}
		};
		table.setFont(new Font("Tahoma", Font.PLAIN, 13));
		table.setModel(tableModel);
		table.setEnabled(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@SuppressWarnings("rawtypes")
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				int viewRow = table.getSelectedRow();
				if(viewRow>-1){
					modelRow = table.convertRowIndexToModel(viewRow);
					
					Vector v = (Vector) tableModel.getRowVector().get(modelRow);
					if(v!=null){
						
					}
					
				}
			}
		});
		scrollPane.setViewportView(table);
		
		return scrollPane;
	}
	
	private JPanel panel_9(){
		JPanel panel_9 = new JPanel();
		GridLayout gl_panel_9 = new GridLayout(1, 2);
		gl_panel_9.setHgap(3);
		gl_panel_9.setVgap(2);
		panel_9.setLayout(gl_panel_9);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_9.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblDokter = new JLabel("Dokter");
		lblDokter.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDokter.setBounds(5, 6, 46, 14);
		panel_1.add(lblDokter);
		
		textFieldDokter = new JTextField();
		textFieldDokter.setEditable(false);
		textFieldDokter.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldDokter.setBounds(81, 5, 186, 23);
		textFieldDokter.setColumns(10);
		panel_1.add(textFieldDokter);
		
		btnAddEditDokter = new JButton("Cari");
		btnAddEditDokter.setFont(new Font("Tahoma", Font.PLAIN, 10));
		btnAddEditDokter.setBounds(144, 30, 60, 23);
		btnAddEditDokter.addActionListener(this);
		panel_1.add(btnAddEditDokter);
		
		btnDelDokter = new JButton("Del");
		btnDelDokter.setFont(new Font("Tahoma", Font.PLAIN, 10));
		btnDelDokter.setBounds(206, 30, 60, 23);
		btnDelDokter.addActionListener(this);
		panel_1.add(btnDelDokter);
		
		JLabel lblCustomer = new JLabel("Customer");
		lblCustomer.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCustomer.setBounds(5, 81, 80, 14);
		panel_1.add(lblCustomer);
		
		textFieldCust = new JTextField();
		textFieldCust.setToolTipText("Type for new customer");
		textFieldCust.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldCust.setBounds(81, 79, 186, 23);
		panel_1.add(textFieldCust);
		textFieldCust.setColumns(10);
		
		btnAddEditCust = new JButton("Cari");
		btnAddEditCust.setFont(new Font("Tahoma", Font.PLAIN, 10));
		btnAddEditCust.setBounds(144, 102, 60, 23);
		btnAddEditCust.addActionListener(this);
		panel_1.add(btnAddEditCust);
		
		btnDelCust = new JButton("Del");
		btnDelCust.setFont(new Font("Tahoma", Font.PLAIN, 10));
		btnDelCust.setBounds(206, 102, 60, 23);
		btnDelCust.addActionListener(this);
		panel_1.add(btnDelCust);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_9.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 2, 0, 2));
		
		JLabel lblRu = new JLabel(" RU");
		lblRu.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblRu);
		
		textFieldRU = new JTextField();
		textFieldRU.setText("0");
		textFieldRU.setEditable(false);
		textFieldRU.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldRU.setFont(new Font("Tahoma", Font.BOLD, 13));
		textFieldRU.setColumns(10);
		panel_2.add(textFieldRU);
		
		JLabel lblTotalObat = new JLabel(" Total Obat");
		lblTotalObat.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblTotalObat);
		
		textFieldTotalObat = new JTextField();
		textFieldTotalObat.setText("0");
		textFieldTotalObat.setFont(new Font("Tahoma", Font.BOLD, 13));
		textFieldTotalObat.setEditable(false);
		textFieldTotalObat.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldTotalObat.setColumns(10);
		panel_2.add(textFieldTotalObat);
		
		JSeparator separator_1 = new JSeparator();
		panel_2.add(separator_1);
		
		JSeparator separator_2 = new JSeparator();
		panel_2.add(separator_2);
		
		JLabel lblJasaDokter = new JLabel(" Biaya Dokter");
		lblJasaDokter.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblJasaDokter);
		
		textFieldBiayaDokter = new JTextField();
		textFieldBiayaDokter.setText("0");
		textFieldBiayaDokter.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldBiayaDokter.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldBiayaDokter.setColumns(10);
		textFieldBiayaDokter.addFocusListener(this);
		textFieldBiayaDokter.addKeyListener(this);
		panel_2.add(textFieldBiayaDokter);
		
		JLabel lblBiayaTindakan = new JLabel(" Biaya Tindakan");
		lblBiayaTindakan.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblBiayaTindakan);
		
		textFieldBiayaTindakan = new JTextField();
		textFieldBiayaTindakan.setText("0");
		textFieldBiayaTindakan.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldBiayaTindakan.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldBiayaTindakan.setColumns(10);
		textFieldBiayaTindakan.addFocusListener(this);
		textFieldBiayaTindakan.addKeyListener(this);
		panel_2.add(textFieldBiayaTindakan);
		
		JLabel lblBiayaLab = new JLabel(" Biaya Laboratorium   ");
		lblBiayaLab.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblBiayaLab.setHorizontalAlignment(SwingConstants.LEFT);
		panel_2.add(lblBiayaLab);
		
		textFieldBiayaLab = new JTextField();
		textFieldBiayaLab.setText("0");
		textFieldBiayaLab.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldBiayaLab.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldBiayaLab.addFocusListener(this);
		textFieldBiayaLab.addKeyListener(this);
		textFieldBiayaLab.setColumns(10);
		panel_2.add(textFieldBiayaLab);
		
		JLabel lblBiayaAdm = new JLabel(" Biaya Administrasi");
		lblBiayaAdm.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblBiayaAdm);
		
		textFieldBiayaAdm = new JTextField();
		textFieldBiayaAdm.setText("0");
		textFieldBiayaAdm.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldBiayaAdm.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldBiayaAdm.setColumns(10);
		textFieldBiayaAdm.addFocusListener(this);
		textFieldBiayaAdm.addKeyListener(this);
		panel_2.add(textFieldBiayaAdm);
		
		JSeparator separator_3 = new JSeparator();
		panel_2.add(separator_3);
		
		JSeparator separator_4 = new JSeparator();
		panel_2.add(separator_4);
		
		JLabel lblMisc = new JLabel(" Misc");
		lblMisc.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblMisc);
		
		textFieldMisc = new JTextField();
		textFieldMisc.setText("0");
		textFieldMisc.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldMisc.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldMisc.setColumns(10);
		textFieldMisc.addFocusListener(this);
		textFieldMisc.addKeyListener(this);
		panel_2.add(textFieldMisc);
		
		JLabel lblSubtotal = new JLabel(" Sub-Total");
		lblSubtotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblSubtotal);
		
		textFieldSubTotal = new JTextField();
		textFieldSubTotal.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldSubTotal.setText("0");
		textFieldSubTotal.setEditable(false);
		textFieldSubTotal.setFont(new Font("Tahoma", Font.BOLD, 13));
		textFieldSubTotal.setColumns(10);
		panel_2.add(textFieldSubTotal);
		
		JSeparator separator_5 = new JSeparator();
		panel_2.add(separator_5);
		
		JSeparator separator_6 = new JSeparator();
		panel_2.add(separator_6);
		
		JLabel lblDiskon = new JLabel(" Diskon (%)");
		lblDiskon.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblDiskon);
		
		textFieldDiskonPersen = new JTextField();
		textFieldDiskonPersen.setText("0");
		textFieldDiskonPersen.setHorizontalAlignment(SwingConstants.LEFT);
		textFieldDiskonPersen.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldDiskonPersen.setColumns(10);
		textFieldDiskonPersen.addFocusListener(this);
		textFieldDiskonPersen.addKeyListener(this);
		panel_2.add(textFieldDiskonPersen);
		
		JLabel lblDiskonRp = new JLabel(" Diskon (Rp)");
		lblDiskonRp.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblDiskonRp);
		
		textFieldDiskonRp = new JTextField();
		textFieldDiskonRp.setText("0");
		textFieldDiskonRp.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldDiskonRp.setEnabled(false);
		textFieldDiskonRp.setEditable(false);
		textFieldDiskonRp.setFont(new Font("Tahoma", Font.BOLD, 13));
		textFieldDiskonRp.setColumns(10);
		panel_2.add(textFieldDiskonRp);
		
		return panel_9;
	}
	
	
	private boolean checkTypeTrx(){
		String selected = (String) comboBoxTrxType.getSelectedItem();
		if(selected.equalsIgnoreCase("-")){
			JOptionPane.showMessageDialog(null, "Silahkan pilih type transaksi", "Error", JOptionPane.ERROR_MESSAGE);
			comboBoxTrxType.requestFocus();
			return false;
		}
		return true;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private Vector vItemList(String productCode, String productName, int quantity, int hargaUnit, int sc, int discount, int sumHarga){
		Vector v = new Vector();
		v.add(productCode);
		v.add(productName);
		v.add(Integer.toString(quantity));
		v.add(AppUtil.formatStringIDR(hargaUnit));
		v.add(Integer.toString(sc));
		v.add(Integer.toString(discount));
		v.add(AppUtil.formatStringIDR(sumHarga));
		
		return v;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void insertItem(Product product, int quantity){
		
		BigDecimal harga = product.getPriceSell();
		BigDecimal amountSC = BigDecimal.ZERO;
		int discount = 0;
		
		String trxType = (String) comboBoxTrxType.getSelectedItem();
		if(trxType.equalsIgnoreCase(AppUtil.TYPE_TRX_RESEP)){
			amountSC = sCValue();
		}
		
		BigDecimal sumHarga = AppUtil.amountSalesTrxObat(harga, quantity, amountSC, discount);
		
		Vector v = vItemList(product.getProductCode(), product.getProductName(), quantity, harga.intValue(), amountSC.intValue(), discount, sumHarga.intValue());
		
		SalesTrxObat salesTrxObat = new SalesTrxObat();
		salesTrxObat.setSalesTrx(currentSalesTrx);
		salesTrxObat.setProduct(product);
		salesTrxObat.setProductCode(product.getProductCode());
		salesTrxObat.setProductName(product.getProductName());
		salesTrxObat.setAmountUnitPrice(harga);
		salesTrxObat.setQuantity(quantity);
		salesTrxObat.setAmountSC(amountSC);
		salesTrxObat.setAmountTrxObat(sumHarga);
		salesTrxObat.setDiscount(discount);
		salesTrxObat.setAmountDiscount(new BigDecimal(discount));
		
		v.add(salesTrxObat);
		
		tableModel.insertData(v);
		
		SalesTrxObatDao stoDao = new SalesTrxObatDao();
		if(stoDao.saveOrUpdate(salesTrxObat)){
			updateStokProduct(product, quantity);
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateItemSame(int rowIndex, SalesTrxObat salesTrxObat, int quantity){
		Product product 			= salesTrxObat.getProduct();
		BigDecimal hargaUnit 		= product.getPriceSell();
		BigDecimal amountSC 		= salesTrxObat.getAmountSC();
		int diskon 					= salesTrxObat.getDiscount();
		int quantityBefore 			= salesTrxObat.getQuantity();
		int quantityUpdate 			= quantityBefore + quantity;
		
		BigDecimal sumHargaUpdate = AppUtil.amountSalesTrxObat(hargaUnit, quantityUpdate, amountSC, diskon);
		
		salesTrxObat.setQuantity(quantity);
		salesTrxObat.setAmountTrxObat(sumHargaUpdate);
		
		SalesTrxObatDao stoDao = new SalesTrxObatDao();
		if(stoDao.saveOrUpdate(salesTrxObat)){
			Vector v = vItemList(product.getProductCode(), product.getProductName(), quantityUpdate, hargaUnit.intValue(), amountSC.intValue(), diskon, sumHargaUpdate.intValue());
			v.add(salesTrxObat);
			
			tableModel.updateData(rowIndex, v);
			
			updateStokProduct(product, quantity);
		}	
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateItemQuantity(int rowIndex, SalesTrxObat salesTrxObat, int quantity){
		int quantityBefore = salesTrxObat.getQuantity();
		if(quantity!=quantityBefore){
			BigDecimal amountSC 		= salesTrxObat.getAmountSC();
			BigDecimal hargaSatuan 		= salesTrxObat.getAmountUnitPrice();
			int diskon 					= salesTrxObat.getDiscount(); 
			
			BigDecimal sumHargaUpdate = AppUtil.amountSalesTrxObat(hargaSatuan, quantity, amountSC, diskon);
			BigDecimal amountDiscount = AppUtil.amountPersentasi(diskon, sumHargaUpdate, 0);
			
			salesTrxObat.setQuantity(quantity);
			salesTrxObat.setAmountDiscount(amountDiscount);
			salesTrxObat.setAmountTrxObat(sumHargaUpdate);
			
			SalesTrxObatDao stoDao = new SalesTrxObatDao();
			if(stoDao.saveOrUpdate(salesTrxObat)){
				Product product = salesTrxObat.getProduct();
				
				Vector v = vItemList(product.getProductCode(), product.getProductName(), quantity, hargaSatuan.intValue(), amountSC.intValue(), diskon, sumHargaUpdate.intValue());
				v.add(salesTrxObat);
				
				tableModel.updateData(rowIndex, v);
				
				int quantityAfter = quantity-quantityBefore;
				updateStokProduct(product, quantityAfter);
				
			}else{
				JOptionPane.showMessageDialog(null, "Update quantity error", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateItemDiscount(int rowIndex, SalesTrxObat salesTrxObat, int discount, BigDecimal amountDiscount, BigDecimal sumHargaUpdate){
		int discoutBefore = salesTrxObat.getDiscount();
		if(discoutBefore!=discount){
			salesTrxObat.setDiscount(discount);
			salesTrxObat.setAmountDiscount(amountDiscount);
			salesTrxObat.setAmountTrxObat(sumHargaUpdate);
			
			SalesTrxObatDao stoDao = new SalesTrxObatDao();
			if(stoDao.saveOrUpdate(salesTrxObat)){
				Product product = salesTrxObat.getProduct();
				int quantity 	= salesTrxObat.getQuantity();
				int hargaSatuan = salesTrxObat.getAmountUnitPrice().intValue();
				
				Vector v = vItemList(product.getProductCode(), product.getProductName(), quantity, hargaSatuan, salesTrxObat.getAmountSC().intValue(), discount, sumHargaUpdate.intValue());
				v.add(salesTrxObat);
				tableModel.updateData(rowIndex, v);
			}	
		}
	}
	
	private void updateStokProduct(Product product, int quantity){
		int stokUpdate = product.getAvailableStock() - quantity;
		product.setAvailableStock(stokUpdate);
		
		ProductDao productDao = new ProductDao();
		productDao.saveOrUpdate(product);
	}
	
	private void updateTypeTrx(){
		String trxType = (String) comboBoxTrxType.getSelectedItem();
		
		SalesTrx st = this.currentSalesTrx;
		st.setTrxType(trxType);
		
		SalesTrxDao dao = new SalesTrxDao();
		if(dao.saveOrUpdate(st)){
			this.currentSalesTrx = st;
		}
	}
	
	private BigDecimal sCValue(){
		return new BigDecimal(1000);
	}
	
	private BigDecimal amounSubTotal(BigDecimal trxObat, BigDecimal RU, BigDecimal misc, BigDecimal doctorServ, BigDecimal medicalTreatment, 
			BigDecimal labServ, BigDecimal adm){
		BigDecimal result = BigDecimal.ZERO;
		
		BigDecimal sum = trxObat.add(RU).add(misc).add(doctorServ).add(medicalTreatment).add(labServ).add(adm);
		result = sum.setScale(0, RoundingMode.HALF_UP);
		
		return result;
	}
	
	
	private void updateRUAndTotalObat(){
		SalesTrxObatDao stoDao = new SalesTrxObatDao();
		BigDecimal amountTrxObat = stoDao.sumAmountTrxBySalesTrx(currentSalesTrx);
		if(amountTrxObat==null){
			amountTrxObat = BigDecimal.ZERO;
		}
				
		BigDecimal amountTrxObatUpdate = amountTrxObat.setScale(0);
		BigDecimal rounding = AppUtil.roundingDozensUp(amountTrxObatUpdate);
		BigDecimal difference = rounding.subtract(amountTrxObat).setScale(0);
		
		SalesTrx st = this.currentSalesTrx;
		st.setAmountRU(difference);
		st.setAmountSalesObat(amountTrxObat);
		
		SalesTrxDao stDao = new SalesTrxDao();
		if(stDao.saveOrUpdate(st)){
			this.currentSalesTrx = st;
		}
		
		textFieldRU.setText(AppUtil.formatStringIDR(difference.intValue()));
		textFieldTotalObat.setText(AppUtil.formatStringIDR(rounding.intValue()));
		
		updateSubTotalAndTotal();
	}
	
	private BigDecimal subTotal(){
		SalesTrx st = this.currentSalesTrx;
		
		BigDecimal amountObat = st.getAmountSalesObat().setScale(0, RoundingMode.HALF_UP);
		System.out.println("amountObat: "+st.getAmountSalesObat());
		
		String ruStr = textFieldRU.getText();
		if(ruStr.trim().isEmpty()) ruStr = "0";
	
		String miscStr = textFieldMisc.getText();
		if(miscStr.trim().isEmpty()) miscStr = "0";
		
		String doctorServStr = textFieldBiayaDokter.getText();
		if(doctorServStr.trim().isEmpty()) doctorServStr = "0";
		
		String medicalTreatStr = textFieldBiayaTindakan.getText();
		if(medicalTreatStr.trim().isEmpty()) medicalTreatStr="0";
		
		String labServStr = textFieldBiayaLab.getText();
		if(labServStr.trim().isEmpty()) labServStr = "0";
		
		String admStr = textFieldBiayaAdm.getText();
		if(admStr.trim().isEmpty()) admStr = "0";
		
		BigDecimal trxObat 		= new BigDecimal(AppUtil.removeCharacters(amountObat.toString(), "."));
		BigDecimal RU 			= new BigDecimal(AppUtil.removeCharacters(ruStr, "."));
		BigDecimal misc 		= new BigDecimal(AppUtil.removeCharacters(miscStr, "."));
		BigDecimal doctorServ 	= new BigDecimal(AppUtil.removeCharacters(doctorServStr, "."));
		BigDecimal medicTreat	= new BigDecimal(AppUtil.removeCharacters(medicalTreatStr, "."));
		BigDecimal labServ 		= new BigDecimal(AppUtil.removeCharacters(labServStr, "."));
		BigDecimal adm 			= new BigDecimal(AppUtil.removeCharacters(admStr, "."));
		BigDecimal subTotal 	= amounSubTotal(trxObat, RU, misc, doctorServ, medicTreat, labServ, adm);
		
		return subTotal;
	}
	
	private BigDecimal discountAmountTrx(){
		BigDecimal subTotal = subTotal();
		
		String discountStr = textFieldDiskonPersen.getText();
		if(discountStr.trim().isEmpty()) discountStr = "0";
		
		int discount = Integer.parseInt(discountStr);
		BigDecimal discountAmmount = AppUtil.amountPersentasi(discount, subTotal, 0);
		
		return discountAmmount.setScale(0, RoundingMode.HALF_UP);
	}
	
	private void updateSubTotalAndTotal(){
		BigDecimal subTotal = subTotal();
		System.out.println("subTotal: "+subTotal);
		BigDecimal discountAmmount = discountAmountTrx();
		System.out.println("discountAmmount: "+discountAmmount);
		
		BigDecimal total = subTotal.subtract(discountAmmount).setScale(0, RoundingMode.HALF_UP);
		
		SalesTrx st = this.currentSalesTrx;
		st.setAmountTrx(total);
		
		SalesTrxDao dao = new SalesTrxDao();
		if(dao.saveOrUpdate(st)){
			this.currentSalesTrx = st;
			textFieldSubTotal.setText(AppUtil.formatStringIDR(subTotal.intValue()));
			lblTotal.setText(AppUtil.formatStringIDR(total.intValue()));			
		}
	}
	
	
	@SuppressWarnings("rawtypes")
	@Override
	public void actionPerformed(ActionEvent ae) {
		Object obj = ae.getSource();
		
		if(obj==btnTrxNew){
			TrxUniqueNumberBean tunb = AppUtil.generateTrxUniqueNumber("D");
			if(tunb==null){
				JOptionPane.showMessageDialog(null, "Generate Nomor Nota error", "Error", JOptionPane.ERROR_MESSAGE);
			}else{
				lblNoNotaValue.setEnabled(true);
				//lblNoNotaValue.setText(tunb.getUniqueNumber());
				comboBoxTrxType.setEnabled(true);
				comboBoxTrxType.requestFocus();
				btnTrxNew.setEnabled(false);
				btnBayar.setEnabled(true);
				btnBatal.setEnabled(true);
				btnAddProd.setEnabled(true);
				btnEditQuantity.setEnabled(true);
				btnEditDiscount.setEnabled(true);
				btnHapusProd.setEnabled(true);
				
				TransactionUniqueNumber tun = new TransactionUniqueNumber();
				tun.setUniqueNumberId(tunb.getId());
				tun.setCode(tunb.getCode());
				tun.setYear(tunb.getYear());
				tun.setMonth(tunb.getMonth());
				tun.setNumber(tunb.getNumber());
				
				TransactionUniqueNumberDao tunDao = new TransactionUniqueNumberDao();
				tunDao.saveOrUpdate(tun);
				
				String timeStart = CalendarUtil.dateToString_HHMM(shift.getStartTime());
				String timeFinish = CalendarUtil.dateToString_HHMM(shift.getFinishTime());
				
				SalesTrx st = new SalesTrx();
				st.setTrxCode(tunb.getUniqueNumber());
				st.setShiftName(shift.getShiftName());
				st.setShiftTime(timeStart+"-"+timeFinish);
				st.setPaymentStatus(AppUtil.TRANSAKSI_STATUS_BARU);
				st.setAmountSalesObat(BigDecimal.ZERO);
				st.setAmountDokterService(BigDecimal.ZERO);
				st.setAmountMedicalTreatment(BigDecimal.ZERO);
				st.setAmountLabService(BigDecimal.ZERO);
				st.setAmountAdministration(BigDecimal.ZERO);
				st.setAmountRU(BigDecimal.ZERO);
				st.setAmountMisc(BigDecimal.ZERO);
				st.setAmountTrx(BigDecimal.ZERO);
				//st.setDiscount(0);
				//st.setAmountDiscount(BigDecimal.ZERO);
				st.setAmountRefund(BigDecimal.ZERO);
				st.setCreatedDate(Calendar.getInstance(CalendarUtil.tx).getTime());
				st.setCreatedBy(user);
				
				SalesTrxDao stDao = new SalesTrxDao();
				if(stDao.saveOrUpdate(st)){
					this.currentSalesTrx = st;
				}
				
			}
		}else if(obj==btnBatal){
			lblNoNotaValue.setEnabled(true);
			lblNoNotaValue.setText("-");
			
			SalesTrx st = this.currentSalesTrx;
			st.setPaymentStatus(AppUtil.TRANSAKSI_STATUS_BATAL);
			
			SalesTrxDao dao = new SalesTrxDao();
			dao.saveOrUpdate(st);
			
			firstConditionForComp();
		}else if(obj==btnBayar){
			TrxUniqueNumberBean tunb = AppUtil.generateTrxUniqueNumber("T");
			if(tunb==null){
				JOptionPane.showMessageDialog(null, "Generate Nomor Nota error", "Error", JOptionPane.ERROR_MESSAGE);
			}else{
				/*TransaksiUniqueNumber tun = new TransaksiUniqueNumber();
				tun.setUniqueNumberId(tunb.getId());
				tun.setCode(tunb.getCode());
				tun.setNumber(tunb.getNumber());
				tun.setMonth(tunb.getMonth());
				tun.setYear(tunb.getYear());
				
				TransactionUniqueNumberDao tunDao = new TransactionUniqueNumberDao();
				tunDao.saveOrUpdate(tun);
				
				SalesTrx st = this.currentSalesTrx;
				st.setTrxCode(tunb.getUniqueNumber());
				
				SalesTrxDao stDao = new SalesTrxDao();
				if(stDao.saveOrUpdate(st)){
					this.currentSalesTrx = st;
					
					lblNoNotaValue.setText(tunb.getUniqueNumber());
				}*/
				
				//firstConditionForComp();
				
				//BayarDialog dialog = new BayarDialog(this, currentSalesTrx);
				//dialog.setVisible(true);
				//dialog.requestFocus();
			}
		}else if(obj==btnAddProd){
			if(!checkTypeTrx()){
				return;
			}
			AddProductToTrxDialog dialog = new AddProductToTrxDialog(this);
			dialog.setVisible(true);
			dialog.requestFocus();
		}else if(obj==btnEditQuantity){
			if(this.modelRow==-1){
				JOptionPane.showMessageDialog(null, "Silahkan pilih satu item", "Error", JOptionPane.ERROR_MESSAGE);
			}else{
				Vector v = (Vector) tableModel.getRowVector().elementAt(modelRow);
				SalesTrxObat sto = (SalesTrxObat) v.elementAt(v.size()-1);
				
				EditTrxObatQuantityDialog dialog = new EditTrxObatQuantityDialog(this, modelRow, sto);
				dialog.setVisible(true);
				dialog.requestFocus();
			}
		}else if(obj==btnEditDiscount){
			if(this.modelRow==-1){
				JOptionPane.showMessageDialog(null, "Silahkan pilih satu item", "Error", JOptionPane.ERROR_MESSAGE);
			}else{
				Vector v = (Vector) tableModel.getRowVector().elementAt(modelRow);
				SalesTrxObat sto = (SalesTrxObat) v.elementAt(v.size()-1);
				
				EditTrxObatDiscountDialog dialog = new EditTrxObatDiscountDialog(this, modelRow, sto);
				dialog.setVisible(true);
				dialog.requestFocus();
			}
		}else if(obj==btnHapusProd){
			if(this.modelRow==-1){
				JOptionPane.showMessageDialog(null, "Silahkan pilih satu item", "Error", JOptionPane.ERROR_MESSAGE);
			}else{
				Vector v = (Vector) tableModel.getRowVector().elementAt(modelRow);
				SalesTrxObat sto = (SalesTrxObat) v.elementAt(v.size()-1);
				
				SalesTrxObatDao dao = new SalesTrxObatDao();
				if(dao.delete(sto)){
					tableModel.removeRow(modelRow);
					updateRUAndTotalObat();
				}
			}
		}else if(obj==btnAddEditDokter){
			AddDokterToTrxDialog dialog = new AddDokterToTrxDialog(this);
			dialog.setVisible(true);
			dialog.requestFocus();
		}else if(obj==btnDelDokter){
			SalesTrx st = this.currentSalesTrx;
			st.setDoctor(null);
			
			SalesTrxDao dao = new SalesTrxDao();
			if(dao.saveOrUpdate(st)){
				textFieldDokter.setText("");
				btnAddEditDokter.setText("Add");				
			}
		}
	}
	
	@SuppressWarnings({ "rawtypes" })
	@Override
	public void insertProduct(Product product, int quantity) {
		this.modelRow = -1;
		comboBoxTrxType.setEnabled(false);
		updateTypeTrx();
		
		int rowIndex = -1;
		SalesTrxObat sto = null;
		
		Vector vector = tableModel.getRowVector();
		for(int i=0;i<vector.size();i++){
			Vector vIsi = (Vector) vector.elementAt(i);
			sto = (SalesTrxObat) vIsi.elementAt(vIsi.size()-1);
			if(sto.getProduct().getProductCode().equalsIgnoreCase(product.getProductCode())){
				rowIndex = i;
			}
		}
		
		if(rowIndex!=-1){
			updateItemSame(rowIndex, sto, quantity);
		}else{
			insertItem(product, quantity);
		}
		
		updateRUAndTotalObat();
		
	}
	
	@Override
	public void updateProduct(HashMap<String, Object> hm) {
		try {
			int update_for 	 = (int) hm.get("update_for");
			int rowIndex 	 = (int) hm.get("rowIndex");
			SalesTrxObat sto = (SalesTrxObat) hm.get("salesTrxObat");
			
			if(update_for==UPDATE_QUANTITY){
				int quantity = (int) hm.get("quantity");
				updateItemQuantity(rowIndex, sto, quantity);
			}else if(update_for==UPDATE_DISCOUNT){
				int discount = (int) hm.get("discount");
				BigDecimal amountDiscount = (BigDecimal) hm.get("discountAmount");
				BigDecimal sumHargaUpdate = (BigDecimal) hm.get("sumHargaUpdate");
				updateItemDiscount(rowIndex, sto, discount, amountDiscount, sumHargaUpdate);
			}
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Data Update Product Null", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		updateRUAndTotalObat();
	}
	
	
	
	public JSplitPane getSplitPane() {
		return splitPane;
	}

	@Override
	public void focusGained(FocusEvent fe) {
		Object obj = fe.getSource();
		
		if(obj==textFieldBiayaDokter){
			String valStr = textFieldBiayaDokter.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			String removeDot = AppUtil.removeCharacters(valStr, ".");
			textFieldBiayaDokter.setText(removeDot);
			
		}else if(obj==textFieldBiayaTindakan){
			String valStr = textFieldBiayaTindakan.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			String removeDot = AppUtil.removeCharacters(valStr, ".");
			textFieldBiayaTindakan.setText(removeDot);
			
		}else if(obj==textFieldBiayaLab){
			String valStr = textFieldBiayaLab.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			String removeDot = AppUtil.removeCharacters(valStr, ".");
			textFieldBiayaLab.setText(removeDot);
			
		}else if(obj==textFieldBiayaAdm){
			String valStr = textFieldBiayaAdm.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			String removeDot = AppUtil.removeCharacters(valStr, ".");
			textFieldBiayaAdm.setText(removeDot);
			
		}else if(obj==textFieldMisc){
			String valStr = textFieldMisc.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			String removeDot = AppUtil.removeCharacters(valStr, ".");
			textFieldMisc.setText(removeDot);
			
		}else if(obj==textFieldDiskonPersen){
			
		}
	}

	@Override
	public void focusLost(FocusEvent fe) {
		Object obj = fe.getSource();
		if(obj==textFieldBiayaDokter){
			String valStr = textFieldBiayaDokter.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			SalesTrx st = this.currentSalesTrx;
			st.setAmountDokterService(new BigDecimal(valStr));
			
			this.currentSalesTrx = st;
			updateSubTotalAndTotal();
			
			textFieldBiayaDokter.setText(AppUtil.formatStringIDR(Integer.parseInt(valStr)));
			
		}else if(obj==textFieldBiayaTindakan){
			String valStr = textFieldBiayaTindakan.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			SalesTrx st = this.currentSalesTrx;
			st.setAmountMedicalTreatment(new BigDecimal(valStr));
			
			this.currentSalesTrx = st;
			updateSubTotalAndTotal();
			
			textFieldBiayaTindakan.setText(AppUtil.formatStringIDR(Integer.parseInt(valStr)));
			
		}else if(obj==textFieldBiayaLab){
			String valStr = textFieldBiayaLab.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			SalesTrx st = this.currentSalesTrx;
			st.setAmountLabService(new BigDecimal(valStr));
			
			this.currentSalesTrx = st;
			updateSubTotalAndTotal();
			
			textFieldBiayaLab.setText(AppUtil.formatStringIDR(Integer.parseInt(valStr)));
			
		}else if(obj==textFieldBiayaAdm){
			String valStr = textFieldBiayaAdm.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			SalesTrx st = this.currentSalesTrx;
			st.setAmountAdministration(new BigDecimal(valStr));
			
			this.currentSalesTrx = st;
			updateSubTotalAndTotal();
			
			textFieldBiayaAdm.setText(AppUtil.formatStringIDR(Integer.parseInt(valStr)));
			
		}else if(obj==textFieldMisc){
			String valStr = textFieldMisc.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			SalesTrx st = this.currentSalesTrx;
			st.setAmountMisc(new BigDecimal(valStr));
			
			this.currentSalesTrx = st;
			updateSubTotalAndTotal();
			
			textFieldMisc.setText(AppUtil.formatStringIDR(Integer.parseInt(valStr)));
		}else if(obj==textFieldDiskonPersen){
			String valStr = textFieldDiskonPersen.getText();
			if(valStr.trim().isEmpty()){
				valStr = "0";
			}
			
			//BigDecimal discountAmmount = discountAmountTrx();
			
			SalesTrx st = this.currentSalesTrx;
			//st.setDiscount(Integer.parseInt(valStr));
			//st.setAmountDiscount(discountAmmount);
			
			this.currentSalesTrx = st;
			updateSubTotalAndTotal();
		}
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		System.out.println("keCode: "+ke.getKeyCode());
	}

	@Override
	public void keyReleased(KeyEvent ke) {
		Object obj = ke.getSource();
		
		if(ke.getKeyCode()==KeyEvent.VK_0 || ke.getKeyCode()==KeyEvent.VK_1 || ke.getKeyCode()==KeyEvent.VK_2 || 
				ke.getKeyCode()==KeyEvent.VK_3 || ke.getKeyCode()==KeyEvent.VK_4 || ke.getKeyCode()==KeyEvent.VK_5 || 
				ke.getKeyCode()==KeyEvent.VK_6 || ke.getKeyCode()==KeyEvent.VK_7 || ke.getKeyCode()==KeyEvent.VK_8 || 
				ke.getKeyCode()==KeyEvent.VK_9 || ke.getKeyCode()==KeyEvent.VK_BACK_SPACE || ke.getKeyCode()==KeyEvent.VK_DELETE){
			
			BigDecimal subTotalGet = subTotal();
			BigDecimal discountAmount = discountAmountTrx();
			BigDecimal total = subTotalGet.subtract(discountAmount).setScale(0, RoundingMode.HALF_UP);
			
			if(obj==textFieldDiskonPersen){
				String valStr = textFieldDiskonPersen.getText();
				if(valStr.trim().isEmpty()){
					valStr = "0";
				}
				
				if(Integer.parseInt(valStr)>100){
					JOptionPane.showMessageDialog(null, "Discount Value invalid", "Error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				textFieldDiskonRp.setText(AppUtil.formatStringIDR(discountAmount.intValue()));
			}
			
			textFieldSubTotal.setText(AppUtil.formatStringIDR(subTotalGet.intValue()));
			lblTotal.setText(AppUtil.formatStringIDR(total.intValue()));
		}
	}

	@Override
	public void keyTyped(KeyEvent ke) {
		// TODO Auto-generated method stub
	}

	@Override
	public void insertDokter(HashMap<String, Object> hm) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void insertCustomer(HashMap<String, Object> hm) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void bayar(HashMap<String, Object> hm) {
		// TODO Auto-generated method stub
		
	}
}
